﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Handrise.CDCable.Mobile.Business;
using System.IO;
using HandRise.MBHOP.Controls;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmRcrdPlyBck : FrmBase
    {
        private LinkFile linkFile;

        private Plan plan = null;

        private LinkFileService linkFileService = new LinkFileService();

        public FrmRcrdPlyBck()
        {
            InitializeComponent();
            ttlbrhead.Text = "录音详情";
            ilstbcup.Images.Add(ResourceImage.修改01);
            ilstbcup.Images.Add(ResourceImage.删除01);
            ilstbcup.Images.Add(ResourceImage.返回01);
            ilstdown.Images.Add(ResourceImage.修改02);
            ilstdown.Images.Add(ResourceImage.删除02);
            ilstdown.Images.Add(ResourceImage.返回02);
        }

        private void FrmRcrdPlyBck_Load(object sender, EventArgs e)
        {
            linkFileService.OnPlaying += new PlayingHandler(linkFileService_OnPlaying);

            if (IsReturn)
                LoadCacheData();
            else
                LoadData();

            tbRecordTitle.Text = linkFile.Title;

            if (File.Exists(linkFile.FullFileName))
            {
                FileInfo fileInfo = new FileInfo(linkFile.FullFileName);
                int len = (int)(fileInfo.Length / 1600 );
                lblRecordLength.Text = FormatSec(len);
                lblFileSize.Text = (fileInfo.Length / 1024).ToString("F2") + "KB";
                pbPlayProgress.Maximum = len;
            }
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            linkFile = (LinkFile)Parameter[0];

            if (Parameter.Count > 1)
                plan = (Plan)Parameter[1];
        }

        /// <summary>
        /// 加载缓存数据
        /// </summary>
        private void LoadCacheData()
        {
            linkFile = (LinkFile)Caches[0];

            if (Caches.Count > 1)
                plan = (Plan)Caches[1];
        }

        /// <summary>
        /// 格式化秒未00:00格式
        /// </summary>
        /// <param name="sec"></param>
        /// <returns></returns>
        private string FormatSec(int sec)
        {
            int min = (sec / 60);
            int restSec = (sec % 60);
            return String.Format("{0:D2}", min) + ":" + String.Format("{0:D2}", restSec);
        }


        void linkFileService_OnPlaying(int playSec, bool isStop)
        {
            pbPlayProgress.Value = playSec;

            if (isStop)
                pbPlayProgress.Value = pbPlayProgress.Maximum;
        }

        private void DeleteRecord()
        {
            if (linkFile.ReportState == "已上报")
            {
                mssdlgComfirm.ShowDialog(this, "当前录音已经上报，不能删除", DialogType.Confirm, FormManage.form_Deactivate);
                return;
            }

            if (plan != null && plan.State == "已完成")
            {
                mssdlgComfirm.ShowDialog(this, "当前计划已完成，录音不能删除", DialogType.Confirm, FormManage.form_Deactivate);
                return;
            }

            string state = linkFileService.GetAudioState();
            if (state != "Init")
                return;

            if (mssdlgexec.ShowDialog(this, "确认删除该录音？", DialogType.Select, FormManage.form_Deactivate) == MessageDialogResult.Button1)
            {
                linkFileService.DeleteLinkFile(linkFile.LinkFileID);
                FormManage.GotoNext("FrmRecordLst", null, null, true);
            }
        }

        private void EditRecord()
        {
            if (linkFile.ReportState == "已上报")
            {
                mssdlgComfirm.ShowDialog(this, "当前录音已经上报，不能修改", DialogType.Confirm, FormManage.form_Deactivate);
                return;
            }

            if (plan != null && plan.State == "已完成")
            {
                mssdlgComfirm.ShowDialog(this, "当前计划已完成，录音不能修改", DialogType.Confirm, FormManage.form_Deactivate);
                return;
            }

            FormManage.GotoNext("FrmEditRecord", new object[] { linkFile }, new object[] { linkFile,plan }, false);
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            string state = linkFileService.GetAudioState();
            if (state != "Init")
                return;

            if (linkFile == null)
                return;

            if (File.Exists(linkFile.FullFileName))
                linkFileService.Play(linkFile.FullFileName);
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            string state = linkFileService.GetAudioState();
            switch (state)
            {
                case "Playing":  //正在播放
                    linkFileService.PausePlay();
                    break;
                case "PlayPause":  //播放暂停
                    linkFileService.RestartPlay();
                    break;
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            string state = linkFileService.GetAudioState();

            if (state == "Playing" || state == "PlayPause")
            {
                //正在播放或暂停播放
                linkFileService.StopPlay();

            }
        }

        private void bcOperate_OnButtonClick(object sender, int buttonIndex)
        {
            //如果正在播放，先停止
            string state = linkFileService.GetAudioState();
            if (state != "Init")
                btnStop_Click(sender, null);

            switch (buttonIndex)
            {
                case 0: //修改
                    EditRecord();
                    break;
                case 1: //删除
                    DeleteRecord();
                    break;
                case 2:
                    FormManage.GotoNext("FrmRecordLst", null, null, true);
                    break;
            }
        }



    }
}